'use strict';

const Service = require('egg').Service;
const Excel = require('exceljs');

class DownloadService extends Service {
  async xls() {
    const workbook = new Excel.Workbook();
    const sheet = workbook.addWorksheet();
    sheet.columns = [
      { header: 'ID', key: 'ID' },
      { header: 'Name', key: 'Name' },
      { header: 'CountryCode', key: 'CountryCode' },
      { header: 'District', key: 'District' },
      { header: 'Population', key: 'Population' },
    ];
    const users = await this.app.mysql.select('city');
    users.forEach(currentValue => {
      sheet.addRow({
        ID: currentValue.ID,
        Name: currentValue.Name,
        CountryCode: currentValue.CountryCode,
        District: currentValue.District,
        Population: currentValue.Population,
      });
    });
    return workbook;
  }
}

module.exports = DownloadService;
